package com.example.spider;

import cn.hutool.core.util.ReUtil;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class Why10PageTester {

    private static RestTemplate rest = getInstance(StandardCharsets.UTF_8);

    public static void main(String[] args) {
        testDetail();
    }

    private static void testNormalPage(){
        String url = "https://10why.net/category/maths";
        String result = rest.getForObject(url, String.class);
        String hasContentRegex = "gridlove-module module-type-posts(.*?)gridlove-pagination gridlove-infinite-scroll";
        List<String> contentStr = ReUtil.findAllGroup1(hasContentRegex, result);
        if(contentStr.isEmpty()){
            // 结束了

        }
        String content = contentStr.get(0);
        String regex = "</a></div><h2 class=\"entry-title h3\"><a href=\"(.*?)\">(.*?)</a></h2><div class=\"entry-content\">";
        //List<String> all = ReUtil.findAll(regex, content, 0);
        List<String> detailUrlList = ReUtil.findAll(regex, content, 1);
        List<String> titleList = ReUtil.findAll(regex, content, 2);
    }

    private static void testEndPage(){
        String url = "https://10why.net/category/maths/page/213213";
        String result = rest.getForObject(url, String.class);
        String hasContentRegex = "gridlove-module module-type-posts(.*?)gridlove-pagination gridlove-infinite-scroll";
        List<String> contentStr = ReUtil.findAllGroup1(hasContentRegex, result);
        if(contentStr.isEmpty()){
            // 结束了

        }
        String content = contentStr.get(0);
        String regex = "</a></div><h2 class=\"entry-title h3\"><a href=\"(.*?)\">(.*?)</a></h2><div class=\"entry-content\">";
        //List<String> all = ReUtil.findAll(regex, content, 0);
        List<String> detailUrlList = ReUtil.findAll(regex, content, 1);
        List<String> titleList = ReUtil.findAll(regex, content, 2);
    }

    private static void testDetail(){
        String url = "https://10why.net/post/4013.html";
        String html =  rest.getForObject(url, String.class);
        String hasContentRegex = "gridlove-sidebar-right(.*)<div class=\"copyright\">";
        List<String> contentStr = ReUtil.findAllGroup1(hasContentRegex, html);
        if(contentStr.isEmpty()){
            // 结束了

        }
        String content = contentStr.get(0);
        String titleRegex = "<h1 class=\"entry-title\">(.*)</h1>";
        String title =  ReUtil.findAllGroup1(titleRegex, content).get(0);

        String imageUrlRex = "src=\"(.*?)\" class=\"attachment-gridlove-single size-gridlove-single wp-post-image";
        String imageUrl = ReUtil.findAllGroup1(imageUrlRex, content).get(0);

        String categoryRegex = "class=\"gridlove-cat gridlove-cat-12\">(.*?)</a>";
        //String category =  ReUtil.findAllGroup1(categoryRegex, content).get(0);

        String idRegex = "article id=\"(.*?)\"";
        String id = ReUtil.findAllGroup1(idRegex, content).get(0);

        String contentRegex = "<div class=\"entry-content\"><p>(.*?)</p><div class=\"entry-tags\">";
        List<String> contentFindResult = ReUtil.findAllGroup1(contentRegex, content);
        String realContent = "";
        if(CollectionUtils.isEmpty(contentFindResult)) {
            //log.warn("empty");
        } else {
            realContent = contentFindResult.get(0);
        }
        // 去 tag 标签
        String xxx = "<a href=\"https://10why.net/tag/%e5%8e%86%e6%b3%95\" title=\"More Posts with the &#039;历法&#039; Tag\">历法</a> <a "
                     + "href=\"https://10why.net/tag/%e6%98%9f%e6%9c%9f\" title=\"More Posts with the &#039;星期&#039; "
                     + "Tag\">星期</a></div></div>";
        realContent = realContent.replaceAll("<a href=.* target=\"_blank\">(.*?)</a>", "$1");
        String tagRegex = "<a href=\".*?&#039; Tag\">(.*?)</a>";
        List<String> tagList = ReUtil.findAllGroup1(tagRegex, content);

        System.out.println(tagList);
    }

    public static RestTemplate getInstance(Charset charset) {
        RestTemplate restTemplate = new RestTemplate();
        List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters();
        for (HttpMessageConverter<?> httpMessageConverter : list) {
            if(httpMessageConverter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(charset);
                break;
            }
        }
        return restTemplate;
    }

}
